﻿' 
' AnyRip (C) 2011 Rick Winkler
'
' This file is part of AnyRip.
' 
' AnyRip is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' AnyRip is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with AnyRip.  If not, see <http://www.gnu.org/licenses/>.
'
Module Main

    Private _Drive As String
    Private _Title As String
    Private _DiscId As String

    Sub Main()

        ' Display Header
        Utils.WriteHeader()

        ' Load Settings File
        If IsNothing(Utils.GetArgument("--settings")) Then
            Utils.LoadSettingsFile(System.AppDomain.CurrentDomain.BaseDirectory & "settings.xml")
        Else
            Utils.LoadSettingsFile(Utils.GetArgument("--settings"))
        End If

        ' Correct drive format and set _Drive
        Select Case Strings.Len(Utils.GetSetting("drive"))
            Case 1
                _Drive = Utils.GetSetting("drive") & ":\"
            Case 2
                _Drive = Utils.GetSetting("drive") & "\"
            Case Else
                _Drive = Utils.GetSetting("drive")
        End Select

        ' Disable AnyDVD (To get proper DiscId)
        AnyDVD.Disable()

        ' Get DiscId
        If IsNothing(MyMovies.GetDiscId(_Drive)) Then
            _DiscId = MyMovies.GetDiscId(_Drive)
        End If

        ' Optionally Get title from DiscId
        If IsNothing(_Title) And Convert.ToBoolean(Utils.GetSetting("lookupbyid")) Then
            _Title = MyMovies.GetDiscTitleByDiscIds(Utils.GetSetting("username"), Utils.GetSetting("password"), _DiscId, Utils.GetSetting("country"))
            Utils.WriteLine("Found title " & Chr(34) & _Title & Chr(34) & " using DiscId.")
        Else
            Utils.WriteLine("Could not find title using DiscId.")
        End If

        ' Optionally lookup title by DiscTitle
        If IsNothing(_Title) And Convert.ToBoolean(Utils.GetSetting("lookupbytitle")) Then
            _Title = MyMovies.GetDiscTitleByDiscTitle(Utils.GetSetting("username"), Utils.GetSetting("password"), _Drive, Utils.GetSetting("country"))
            Utils.WriteLine("Found title " & Chr(34) & _Title & Chr(34) & " using Disc Title.")
        Else
            Utils.WriteLine("Could not find title using Disc Title.")
        End If

        ' Optionally use DiscTitle
        If IsNothing(_Title) And Convert.ToBoolean(Utils.GetSetting("ripifnotfound")) Then
            _Title = MyMovies.GetBasicTitle(_Drive)
            Utils.WriteLine("Using disc title " & Chr(34) & _Title & Chr(34) & " for title.")
        End If

        ' Exit if _Title still not found
        If IsNothing(_Title) Then
            Utils.WriteLine("Could not determin disc title. Exiting...")
            Environment.Exit(1)
        End If

        ' Enable AnyDVD
        AnyDVD.Enable()

        ' Display Remaining Lookups
        MyMovies.ShowRemainingLookups(Utils.GetSetting("username"), Utils.GetSetting("password"))

        ' Rip Disc and Eject if Specified
        If AnyDVD.RipDisc(_Drive, _Title, Utils.GetSetting("rippath")) Then
            If Convert.ToBoolean(Utils.GetSetting("ejectonsuccess")) Then
            End If
        End If

    End Sub

End Module
